<?php

namespace app\live\controller;

use tencentcloud\Stream;
use app\common\model\live\RoomModel;

class MixedController extends BaseController
{

  private $stream;
  private $trtcConfig;
  private $roomModel;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->stream     = new Stream();
    $this->trtcConfig = config('cloud.tencent.trtc');
    $this->roomModel  = new RoomModel();
  }

  /**
   * 开始混流
   */
  public function begin()
  {
    $pre         = $this->trtcConfig['appid']; //流名称前缀
    $roomId      = $this->request->param('room_id/d');
    $hostName    = $this->request->param('host_name/s');
    $hostUserId  = "host" . md5_16($roomId . $hostName); //主持人UserId
    $outStreamId = "{$pre}_{$roomId}_{$hostUserId}_main"; //输出流Id
    $mainUserId  = $this->request->param('main_userid'); //主屏流ID
    $userIds     = $this->request->param('user_ids');
    $userIdsArr    = explode(',', $userIds);
    $userIdsArr    = array_unique($userIdsArr);
    $noVideo = $this->request->param('no_video'); //无视频画面的用户ID组
    $noVideo = explode(',',$noVideo);
    if (!in_array($hostUserId, $userIdsArr)) abort(500, '必须包括主持人流');
    if ($mainUserId != $hostUserId && count($userIdsArr) == 1) abort(500, '只有一路流时必须为主持人流');
    if (count($userIdsArr) == 1) { //只有一路的时候取消混流
      $result = $this->stream->cancelMixStream($outStreamId);
      $this->success('操作成功', null, ['action' => '取消混流', 'result' => $result, 'request' => $this->request->request()]);
    }
    //主屏参数
    $inputParams[] = [
        "input_stream_id" => "{$pre}_{$roomId}_{$mainUserId}_main",
        "layout_params" => [
            "image_layer" => 1,
        ]
    ];
    $layoutParams  = [
        2 => ["image_layer" => 2, "image_width" => 0.19, "image_height" => 0.20, "location_x" => 0, "location_y" => 0.80],
        3 => ["image_layer" => 3, "image_width" => 0.19, "image_height" => 0.20, "location_x" => 0.2, "location_y" => 0.80],
        4 => ["image_layer" => 4, "image_width" => 0.19, "image_height" => 0.20, "location_x" => 0.4, "location_y" => 0.80],
        5 => ["image_layer" => 5, "image_width" => 0.19, "image_height" => 0.20, "location_x" => 0.6, "location_y" => 0.80],
        6 => ["image_layer" => 6, "image_width" => 0.19, "image_height" => 0.20, "location_x" => 0.8, "location_y" => 0.80]
    ];
    $layer = 2; //排除输出流起始层
    //小屏参数
    foreach ($userIdsArr as $userId) {
      if ($userId == $mainUserId) continue; //排除包括主画面的流
      if(in_array($userId,$noVideo)){ //无视频画面的流
        $inputParams[] = [
            "input_stream_id" => "{$pre}_{$roomId}_{$userId}_main",
            "layout_params" => [
                "input_type" => 4, //标识音视频流
                "image_layer" =>$layer
            ]
        ];
      }else{
        $inputParams[] = [
            "input_stream_id" => "{$pre}_{$roomId}_{$userId}_main",
            "layout_params" => $layoutParams[$layer]
        ];
      }
      $layer++;
    }
    $result = $this->stream->startMixStream($outStreamId, $inputParams);
    $this->success('操作成功', null, ['action' => '开始混流', 'result' => $result, 'request' => $this->request->request()]);
  }

}